package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import jdk.nashorn.internal.runtime.linker.LinkerCallSite;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

@RestController
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    //分页查询
    @GetMapping("/dish/page")
    public ResultInfo selectByPage(
            String name,
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,//当前页数
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize//当前页的数据数
    ) {
        Page<Dish> dishList = dishService.selectByPage(name, pageNum, pageSize);

        return ResultInfo.success(dishList);
    }

    //根据主键查询
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable("id") Long id) {
        Dish dish = dishService.findById(id);
        return ResultInfo.success(dish);
    }

    //根据id和status=1，获取获取菜品数据
    @GetMapping("/dish/list")
    private ResultInfo findList(String name, Long categoryId,
                                @RequestParam(defaultValue = "1") Integer status) {
        List<Dish> dishList = dishService.findList(name, categoryId, status);
        return ResultInfo.success(dishList);
    }


//******************************下面都是需要进行Redis操作的**************************************

    //保存
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish) {
        //删除一个菜品或套餐
        String key = "dish_" + dish.getCategoryId();
        redisTemplate.delete(key);
        dishService.save(dish);
        return ResultInfo.success(null);
    }

    //更新
    @PutMapping("/dish")
    public ResultInfo update(@RequestBody Dish dish) {
        //删除多个菜品或套餐
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        dishService.update(dish);
        return ResultInfo.success(null);
    }

    //删除方法
    @DeleteMapping("/dish")
    public ResultInfo delete(Long[] ids) {
        //删除多个菜品或套餐;因为它会影响多个
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        if (ids != null && ids.length > 0) {
            dishService.deleteByIds(ids);
        }
        return ResultInfo.success(null);
    }

    //停用启用方法
    @PostMapping("/dish/status/{status}")
    public ResultInfo status(@PathVariable("status") Integer status,
                             @RequestParam("ids") List<Long> ids) {
        //删除多个菜品或套餐;因为它会影响多个
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        dishService.status(status, ids);
        return ResultInfo.success(null);
    }
}
